#/bin/bash
Url=http://mirror.cnop.net
#System 
if [ -e /etc/redhat-release ]; then
    OS=CentOS
	Command="yum -y install gcc wget make cmake gcc-c++ ncurses ncurses-devel bzip2 openssl openssl-devel zlib-devel bison bison-devel libcurl-devel net-tools"
   [ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && OS_Ver=7
   [ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" ] && OS_Ver=6
  elif [ -n "$(grep -i 'Debian' /etc/issue 2> /dev/null)" ]; then
    OS=Debian
	Command="apt-get install make cmake gcc g++ bison libncurses5-dev build-essential net-tools"
  elif [ -n "$(grep -i 'Ubuntu' /etc/issue 2> /dev/null)" ]; then
    OS=Ubuntu
	Command="apt-get -y install make cmake gcc g++ bison libncurses5-dev build-essential libssl-dev pkg-config net-tools"
  else
    echo "UnknownOS"	
 	exit
fi
echo -e "\033[32m $OS $OS_Ver \033[0m"

#Install path

echo "" && echo -e "\033[32m Mysql path ( Default path /usr/local/mysql ): \033[0m"
read Path

if [ -z "$Path" ]; then
  echo "" && echo -e "\033[32m  Using the default path /usr/local/mysql  \033[0m"
  Path=/usr/local/mysql
  echo $Path
else
  echo "" && echo -e "\033[32m ....... Your input $Path ........ \033[0m"
  echo $Path
fi

P='$PATH'
#cpu core
Core=`cat /proc/cpuinfo| grep "processor"| wc -l`
$Command

echo "" && echo -e "\033[32m Zlib install: \033[0m"
wget $Url/lib/zlib/zlib-1.2.11.tar.gz && tar -zxvf zlib-1.2.11.tar.gz && cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make -j$Core && make -j$Core install
cd ..

echo "" && echo -e "\033[32m Jemalloc install: \033[0m"
wget $Url/lib/jemalloc/jemalloc-4.2.1.tar.bz2
tar -xf jemalloc-4.2.1.tar.bz2 && cd jemalloc-4.2.1
./configure && make -j$Core && make -j$Core install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
/sbin/ldconfig
cd ..

echo "" && echo -e "\033[32m Boost install: \033[0m"
wget $Url/lib/boost/boost_1_59_0.tar.gz
tar zxf boost_1_59_0.tar.gz
mv boost_1_59_0  /usr/local/boost

cd ./
wget $Url/mysql/percona-server/percona-server-5.7.29-32.tar.gz 
tar zxf percona-server-5.7.29-32.tar.gz
echo "gmock install:"
cd percona-server-5.7.29-32
mkdir source_downloads
wget -P source_downloads $Url/lib/gmock/release-1.8.0.zip
echo "" && echo -e "\033[32m ....... Create mysql user: ........ \033[0m"
useradd -u 8000 -s /sbin/nologin -M mysql

echo "" && echo -e "\033[32m ....... percona-server install:: ........ \033[0m"
cmake . -DCMAKE_INSTALL_PREFIX=$Path \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=$Path/data \
-DSYSCONFDIR=/etc \
-DMYSQL_TCP_PORT=3306 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_DEBUG=0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_EDITLINE=bundled \
-DWITH_SSL:STRING=system \
-DWITH_ZLIB:STRING=bundled \
-DZLIB_LIBRARY:FILEPATH=/usr/local/zlib/lib/ \
-DZLIB_INCLUDE_DIR:PATH=/usr/local/zlib/include/ \
-DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" \
-DENABLE_DOWNLOADS=1 \
-DWITH_BOOST=/usr/local/boost

make -j$Core && make -j$Core install

chown -R mysql.mysql $Path
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "" && echo -e "\033[32m Mysql password: \033[0m"
$Path/bin/mysqld --initialize --user=mysql  --basedir=$Path/ --datadir=$Path/data/
echo "" && echo -e "\033[32m ...................... \033[0m"
sleep 3
cat >/etc/my.cnf<<EOF
[client]
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = $Path/data
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1024M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 1M
read_rnd_buffer_size = 16M
myisam_sort_buffer_size = 8M
thread_cache_size = 64
query_cache_size = 8M
tmp_table_size = 16M
max_connections = 1000
max_connect_errors = 1000
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id	= 1
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = $Path/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir =  $Path/data
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

EOF

cp  support-files/mysql.server  /etc/init.d/mysqld
echo "
PATH=$Path/bin:$Path/lib:$P
export PATH
" >>/etc/profile
echo "
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
">> /etc/security/limits.conf
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf 
sysctl -p
source /etc/profile
ln -s $Path/bin/mysql /usr/bin
echo "" && echo -e "\033[32m Starting: \033[0m"
chmod +x $Path/bin/mysql && chmod +x /etc/init.d/mysqld
service mysqld start
chkconfig --level 35 mysqld on
netstat -tulnp | grep 3306
#cat /root/.mysql_secret
echo -e "\033[32m Done \033[0m"